home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / AMarquee / examples / amarqueehost.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  4KB  |  125 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. struct Library * AMarqueeBase = NULL;
  15. struct QSession * session     = NULL;
  16.  
  17. /* Takes user input until blank line is typed */
  18. void ProcessDebugCommands(struct QSession * session)
  19. {
  20.   while(1)
  21.   {
  22.    char debugline[500] = "\0\0\0\0\0\0\0\0";
  23.    char * keyword, * data;
  24.    ULONG dataLen = 0L;
  25.    LONG res;
  26.    
  27.    printf("Enter your debug command now: "); fflush(stdout);
  28.    gets(debugline);
  29.   
  30.    keyword = &debugline[2];
  31.    if (data = strchr(keyword,'='))
  32.    {
  33.      *data = '\0';  /* terminate the keyword */
  34.      data++;
  35.      dataLen = strlen(data)+1;
  36.    }
  37.    switch((int)(debugline[0]))
  38.    {
  39.      case '\0': res=QGo(session,0L);                  break;
  40.      case 'a':  res=QSetAccessOp(session, keyword);          break;
  41.      case 's':  res=QSetOp(session, keyword, data, dataLen); break;
  42.      case 'r':  res=QRenameOp(session, keyword, data);       break;
  43.      case 'D':  res=QDebugOp(session, keyword);              break;
  44.      case 'g':  res=QGetOp(session, keyword, -1);            break;
  45.      case 'd':  res=QDeleteOp(session, keyword);             break;
  46.      case 'i':  res=QInfoOp(session);                        break;
  47.      case 'c':  res=QSubscribeOp(session, keyword, -1);      break;
  48.      case 'k':  res=QClearSubscriptionsOp(session,atoi(keyword)); break;
  49.      case 'p':  res=QPingOp(session);                        break;
  50.      default:   printf("Command code %c was not recognized.\n",debugline[0]); break;
  51.    }
  52.    printf("(Op result was %i)\n",res);
  53.    if (debugline[0] == '\0') return;
  54.   }
  55. }
  56.  
  57.  
  58. void CleanExit(void)
  59. {
  60.   if (session)      QFreeSession(session);        /* This MUST be done before we close the library! */
  61.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);  
  62.   printf("All done.\n");
  63. }
  64.  
  65. /* Main program */
  66. int main(int argc, char ** argv)
  67. {
  68.   char * acceptFrom, * progNames;
  69.   int port;
  70.   
  71.   printf("Usage Note:  AMarqueeHost [hosts=#?] [programs=#?] [port=INADDR_ANY]\n");  
  72.   atexit(CleanExit);
  73.   
  74.   acceptFrom = (argc > 1) ? argv[1] : "#?";
  75.   progNames  = (argc > 2) ? argv[2] : "#?";
  76.   port       = (argc > 3) ? atoi(argv[3]) : 0;
  77.   
  78.   if ((AMarqueeBase = OpenLibrary("amarquee.library",37L)) == NULL)
  79.   {
  80.     printf("Couldn't open amarquee.library v37!\n");
  81.     exit(RETURN_ERROR);
  82.   }
  83.   if ((session = QNewHostSession(acceptFrom, &port, progNames)) == NULL)
  84.   {
  85.     printf("Couldn't open host session on port %i.\n",port);
  86.     exit(RETURN_WARN);
  87.   }
  88.   printf("Accepting connections from %s:%s on port %i.\n",acceptFrom, progNames,port);
  89.   
  90.   printf("Press CTRL-F to enter commands, or CTRL-C to quit\n");
  91.   
  92.   while(1)
  93.   {
  94.     struct QMessage * qMsg;
  95.     ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C) | (SIGBREAKF_CTRL_F);
  96.  
  97.     /* Wait for next message from the server */
  98.     signals = Wait(signals);
  99.     
  100.     if (signals & (1L << session->qMsgPort->mp_SigBit))
  101.     {
  102.       while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  103.       {
  104.         struct QRunInfo * inf = (qMsg->qm_DataLen == sizeof(struct QRunInfo)) ? (struct QRunInfo *) qMsg->qm_Data : NULL;
  105.  
  106.         /* Handle message */
  107.         printf("Message %p recieved---------\n",qMsg);
  108.         printf("Status:       %i\n",  qMsg->qm_Status);
  109.         printf("Error Line:   %i\n",  qMsg->qm_ErrorLine);
  110.         printf("Message ID:   %i\n",  qMsg->qm_ID);
  111.         printf("Path:        [%s]\n", qMsg->qm_Path?qMsg->qm_Path:"<NULL>");
  112.         printf("Data:        [%s](int=%i)\n", qMsg->qm_Data?qMsg->qm_Data:((UBYTE*)"<NULL>"),qMsg->qm_Data?(*((int*)qMsg->qm_Data)):0);
  113.         printf("DataLen:      %lu\n", qMsg->qm_DataLen);
  114.         printf("ActualLen:    %lu\n", qMsg->qm_ActualLen);
  115.  
  116.         if (inf) printf("Info: alloced:%li allowed:%li avail:%li\n",inf->qr_Alloced, inf->qr_Allowed, inf->qr_Avail); 
  117.         FreeQMessage(session, qMsg);
  118.       }
  119.     }
  120.     if (signals & SIGBREAKF_CTRL_F) ProcessDebugCommands(session);
  121.     if (signals & SIGBREAKF_CTRL_C) break;  /* Quit if CTRL-C pressed */
  122.   }
  123.   /* CleanExit() called here! */
  124. }
  125.